<template>
  <div class="edit-container">
    <el-form v-loading="formLoading" ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="名称" prop="membershipName">
        <el-input placeholder="名称" v-model="form.membershipName" class="col" maxlength="3"/>
      </el-form-item>
      <el-form-item label="福利内容" prop="membershipBenefits">
        <el-input placeholder="福利内容" v-model="form.membershipBenefits" class="col" maxlength="3"/>
      </el-form-item>
      <el-form-item label="支付金额" prop="paymentAmount">
        <el-input type="number" v-model="form.paymentAmount" maxlength="7" placeholder="支付金额"
                  oninput="value=value.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"
                  @blur="form.paymentAmount=$event.target.value"/>
      </el-form-item>
      <el-form-item label="抵用金值" prop="offsetAmount">
        <el-input type="number" v-model="form.offsetAmount" maxlength="7" placeholder="抵用金值"
                  oninput="value=value.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"
                  @blur="form.offsetAmount=$event.target.value"/>
      </el-form-item>
      <el-form-item label="益联券值" prop="voucher">
        <el-input type="number" v-model="form.voucher" maxlength="7" placeholder="益联券值"
                  oninput="value=value.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"
                  @blur="form.voucher=$event.target.value"/>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input type="number" v-model="form.sort" maxlength="5" placeholder="排序"
                  oninput="value=value.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"
                  @blur="form.sort=$event.target.value"/>
      </el-form-item>
      <el-form-item label="描述内容" prop="content">
        <Tinymce v-model="form.content"></Tinymce>
      </el-form-item>
      <el-form-item>
        <el-button v-loading="saveLoading" element-loading-background="rgba(0, 0, 0, 0.3)" type="primary"
                   @click="handlerSubmit" v-has-permi="['admin:member:level:save']">保存
        </el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {detail, save} from "@/api/member";
import {getForm} from "@/utils/utils";
import Tinymce from "@/components/Tinymce/index.vue";

export default {
  components: {Tinymce},
  props: {
    id: {
      default: null
    }
  },
  data() {
    return {
      form: {
        id: null,
        membershipName: '',
        membershipBenefits: '',
        content: '',
        paymentAmount: 0,
        offsetAmount: null,
        voucher: null,
        sort: 1,
      },
      rules: {
        membershipName: [{required: true, message: '请填写充值会员挡位名称', trigger: ['blur']}],
        content: [{required: true, message: '请填写描述内容', trigger: ['blur']}],
        paymentAmount: [{required: true, message: '请填写需支付金额', trigger: ['blur']}],
        offsetAmount: [{required: true, message: '请填写抵用金值', trigger: ['blur']}],
      },
      roles: [],
      saveLoading: false,
      formLoading: false,
      uploads: {
        show: false,
        multiple: true,
      }
    }
  },
  mounted() {
    if (this.id) {
      this.getDetail();
    }
  },
  methods: {
    getDetail() {
      this.formLoading = true
      detail(this.id).then(res => {
        getForm(res, this.form);
      }).finally(() => {
        this.formLoading = false;
      })
    },
    handlerSubmit() {
      if (this.saveLoading || this.formLoading) {
        return;
      }
      this.$refs.form.validate(valid => {
        if (!valid) return;
        this.saveLoading = true;
        save(this.form).then(() => {
          this.$message.success('保存成功');
          this.close();
        }).finally(() => {
          this.saveLoading = false
        })
      })
    },
    close() {
      this.$emit('hideEditDialog')
    }
  }
}
</script>
